package com.longcai.service.impl.exchange;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.constants.BaseConstants;
import com.longcai.entity.exchangemall.ClyjExchangeMall;
import com.longcai.entity.exchangemall.vo.ClyjExchangeMallVo;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homegood.ClyjGoodsPics;
import com.longcai.entity.homegood.ClyjGoodsSpec;
import com.longcai.mapper.exchange.ClyjExchangeMallMapper;
import com.longcai.mapper.homegood.ClyjGoodsHomepageMapper;
import com.longcai.service.exchange.ClyjExchangeMallService;
import com.longcai.service.homegood.ClyjGoodsSpecService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 兑换商城（积分商城，红包商城） 服务实现类
 * </p>
 *
 * @author kh
 * @since 2019-04-15
 */
@Service
public class ClyjExchangeMallServiceImpl extends ServiceImpl<ClyjExchangeMallMapper, ClyjExchangeMall> implements ClyjExchangeMallService {

    private static Logger logger = LoggerFactory.getLogger(ClyjExchangeMallServiceImpl.class);

    @Autowired
    private ClyjExchangeMallMapper clyjExchangeMallMapper;

    @Autowired
    private ClyjGoodsHomepageMapper clyjClyjGoodsHomepageMapper;
    
	
	@Autowired
    private ClyjGoodsSpecService clyjGoodsSpecService;

    @Override
    public ApiResult<?> queryExchangeMallList(String type, String page, String size) {
        Page<ClyjExchangeMall> pages = new Page<>(Long.parseLong(page), Long.parseLong(size));
        List<ClyjExchangeMallVo> list = new ArrayList<ClyjExchangeMallVo>();
        QueryWrapper<ClyjExchangeMall> queryWrapper = new QueryWrapper<ClyjExchangeMall>();
        queryWrapper.eq("type", type);//商城类型（0，积分商城，1，红包商城, 2,礼品商城）
        queryWrapper.eq("state", "1");//状态(0,未审核，1审核通过)
        IPage<ClyjExchangeMall> sysRoleIPage = clyjExchangeMallMapper.selectPage(pages, queryWrapper);
        //List<ClyjExchangeMall>  listClyjExchangeMall=clyjExchangeMallMapper.selectList(queryWrapper);
        try {
            for (ClyjExchangeMall clyjExchangeMall : sysRoleIPage.getRecords()) {
                String goodsId = clyjExchangeMall.getGoodsId();
                QueryWrapper<ClyjGoodsHomepage> q = new QueryWrapper<ClyjGoodsHomepage>();
                q.eq("goods_id", goodsId);
                /*q.eq("goodsStatus", val);//状态是可买的 TODO*/
                ClyjGoodsHomepage clyjGoodsHomepage = clyjClyjGoodsHomepageMapper.selectOne(q);
                ApiResult r1=clyjGoodsSpecService.getGoodsSpec(clyjExchangeMall.getGoodsId());
                ApiResult r2=clyjGoodsSpecService.getSpecPics(clyjExchangeMall.getGoodsId());
	        	if(r1.getCode().equals(BaseConstants.SUCCESS)){
	        		clyjExchangeMall.setClyjGoodsSpecList((List<ClyjGoodsSpec>)r1.getData());
	        	}
	        	if(r2.getCode().equals(BaseConstants.SUCCESS)){
	        		clyjExchangeMall.setClyjGoodsPicsList((List<ClyjGoodsPics>)r2.getData());
	        	}
                list.add(conver(clyjExchangeMall, clyjGoodsHomepage, type));
            }
        } catch (Exception e) {
            logger.error("查询兑换商城失败");
            return ApiResult.fail("查询兑换商城失败"+e.getMessage());
        }

        /**缓存**/
        return ApiResult.success(list);
    }

    /****
     * 组装对象类
     * @param clyjExchangeMall 兑换商城表实体
     * @param clyjGoodsHomepage 首页商品表实体
     * @param type 商城类型 （0，积分商城，1，红包商城, 2,礼品商城）
     * @return
     */
    public ClyjExchangeMallVo conver(ClyjExchangeMall clyjExchangeMall, ClyjGoodsHomepage clyjGoodsHomepage, String type) {
        ClyjExchangeMallVo clyjExchangeMallVo = new ClyjExchangeMallVo();
        clyjExchangeMallVo.setGoodsId(clyjExchangeMall.getGoodsId());//商品Id
        if (type.equals("0")) {
            clyjExchangeMallVo.setIntegral(clyjExchangeMall.getIntegral());//积分
        } else if (type.equals("1")) {
            clyjExchangeMallVo.setRedPacket(clyjExchangeMall.getRedPacket());//红包
        } else if (type.equals("2")) {
            clyjExchangeMall.setGiftCertificate(clyjExchangeMall.getGiftCertificate());//礼品劵
        }
        clyjExchangeMallVo.setPrice(clyjExchangeMall.getPrice());//价格
        clyjExchangeMallVo.setOriginalPrice(clyjGoodsHomepage.getOriginalPrice());//原价
        clyjExchangeMallVo.setGoodsName(clyjGoodsHomepage.getGoodsName());//商品名称
        clyjExchangeMallVo.setGoodsPic(clyjGoodsHomepage.getGoodsPic());//商品缩略图
        clyjExchangeMallVo.setStock(clyjExchangeMall.getStock());//库存(兑换商城表实体的库存)
        clyjExchangeMallVo.setPostFee(clyjGoodsHomepage.getPostFee());//快递费
        clyjExchangeMallVo.setMonthSales(clyjGoodsHomepage.getMonthSales());//月销量
        clyjExchangeMallVo.setGoodsSpec(clyjGoodsHomepage.getGoodsSpec());//商品规格
        clyjExchangeMallVo.setGoodsDepict(clyjGoodsHomepage.getGoodsDepict());//商品描述
        clyjExchangeMallVo.setGoodsDetail(clyjGoodsHomepage.getGoodsDetail());//商品描述
        clyjExchangeMallVo.setGoodsBanner(clyjGoodsHomepage.getGoodsBanner());//商品banner
        clyjExchangeMallVo.setGoodsBrandId(clyjGoodsHomepage.getGoodsBrandId());//商品品牌id
        clyjExchangeMallVo.setClyjGoodsPicsList(clyjExchangeMall.getClyjGoodsPicsList());
        clyjExchangeMallVo.setClyjGoodsSpecList(clyjExchangeMall.getClyjGoodsSpecList());
        
        return clyjExchangeMallVo;
    }

}
